adminerModule.controller('subscriberList', function($scope, subscriberList, $routeParams) {

  $(function() {
    $('ul.nav li').each(function() {
      $(this).attr('class', 'normal');
      if ($(this).attr('data-name') == 'subscribers') {
        $(this).attr('class', 'active');
      }
    });
  });

  subscriberList.getSubscribers().then(function(subscribers) {
    $scope.subscribers = subscribers;
  });

  $scope.$on('handleSubscribersBroadcast', function(event, subscribers) {
    $scope.subscribers = subscribers;
  });

  $scope.formatTime = function(timestamp) {
    return timestampToTime(timestamp);
  }
});

adminerModule.controller('subscriberListEdit', function($scope, subscriberList, $routeParams) {
  $scope.subscriberId = $routeParams.id;

  $scope.user = subscriberList.getSingleSubscriber($routeParams.id);

  $scope.$on('singleSubscriberChange', function(event, singleSubscriber) {
    $scope.subscriber = singleSubscriber;
    //console.log($scope.subscriber);
  });

  $scope.saveSubscriber = function(subscriber) {
    console.log(123);
    console.log(subscriber);
  }
});

adminerModule.controller('newsletterController', function($scope, $http) {
	$scope.name = 'newsletterController';

	$scope.sendMyMail = function() {
		$scope.getSubscribers = $http.get(base_url + 'ajax/get_newsletter_subscribers').then(function(response) {
			listSubscribers = response.data.subscribers;
			newsletterHTML = response.data.mailhtml;
			subscriberCount = listSubscribers.length;

			for (i = 0; i < subscriberCount; i++) {
				$http({
					headers: {'Content-Type': 'application/x-www-form-urlencoded'},
					url: base_url + 'sendmail.php',
					method: "POST",
					data: $.param({
						'token': 'dfglkdffp934509258dflkaj',
						'to': listSubscribers[i],
//						'to': 'simplyadda.social@gmail.com',
						'subject': 'New post on my site',
						'body' : newsletterHTML
					})
				})
					.success(function(data) {
						$scope.mailContent = $scope.mailContent + 'Mail send to ' + listSubscribers[i];
					});
				console.log($scope.mailContent);
			}
		});
	}
});